package stack_queue.leetcode;

import java.util.Stack;

//检测括号匹配
public class Num20 {
    public boolean isValid(String s) {
        Stack<Character> stack = new Stack<>();
        //字符串转字符输出
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (c == '(' || c == '{' || c == '[') {
                stack.push(c);
            } else {
                //此时c是右括号
                if (stack.isEmpty()) {
                    return false;
                }
                char top = stack.pop();
                if (c == ')' && top != '(') {
                        return false;
                }
                if (c == ']' && top != '[') {
                        return false;
                }
                if (c != '}' && top != '}') {
                        return false;
                }
            }
        }
        return stack.isEmpty();
    }
}
